//
//  NSDate+Compare.m
//  lumihui
//
//  Created by oceans on 2018/12/14.
//  Copyright © 2018 ios. All rights reserved.
//

#import "NSDate+Compare.h"
#import <objc/runtime.h>

@implementation NSDate (Compare)

- (NSString *)compareString {
    
    static NSString *compare = nil;
    return objc_getAssociatedObject(self, @selector(compareString))?:compare;
}

- (void)setCompareString:(NSString *)compareString {
    objc_setAssociatedObject(self, @selector(compareString), compareString, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)nian {
    
    static NSString *nianStr = nil;
    return objc_getAssociatedObject(self, @selector(nian))?:nianStr;
}

- (void)setNian:(NSString *)nian {
    objc_setAssociatedObject(self, @selector(nian), nian, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)yue {
    
    static NSString *yueStr = nil;
    return objc_getAssociatedObject(self, @selector(yue))?:yueStr;
}

- (void)setYue:(NSString *)yue {
    objc_setAssociatedObject(self, @selector(yue), yue, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSString *)ri {
    
    static NSString *riStr = nil;
    return objc_getAssociatedObject(self, @selector(ri))?:riStr;
}

- (void)setRi:(NSString *)ri {
    objc_setAssociatedObject(self, @selector(ri), ri, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (BOOL)isSameMonth:(NSDate *)date format:(NSDateFormatter *)format {
    
    if (!format) {
        return NO;
    }
    if (self.compareString == nil || self.compareString.length == 0) {
        [format setDateFormat:@"yyyy-MM-dd"];
        self.compareString = [format stringFromDate:self];
        NSArray *three = [self.compareString componentsSeparatedByString:@"-"];
        self.nian = three.firstObject;
        self.yue = three[1];
        self.ri = three.lastObject;
    }
    [format setDateFormat:@"MM"];
    NSString *string = [format stringFromDate:date];
    [format setDateFormat:@"yyyy"];
    NSString *nian = [format stringFromDate:date];
    return [self.yue isEqualToString:string] && [self.nian isEqualToString:nian];
}

- (BOOL)isSameDay:(NSDate *)date format:(NSDateFormatter *)format {
    
    if (!format) {
        return NO;
    }
    if (self.compareString == nil || self.compareString.length == 0) {
        [format setDateFormat:@"yyyy-MM-dd"];
        self.compareString = [format stringFromDate:self];
        NSArray *three = [self.compareString componentsSeparatedByString:@"-"];
        self.nian = three.firstObject;
        self.yue = three[1];
        self.ri = three.lastObject;
    }
    [format setDateFormat:@"MM"];
    NSString *string = [format stringFromDate:date];
    [format setDateFormat:@"yyyy"];
    NSString *nian = [format stringFromDate:date];
    [format setDateFormat:@"dd"];
    NSString *ri = [format stringFromDate:date];
    return [self.ri isEqualToString:ri] && [self.yue isEqualToString:string] && [self.nian isEqualToString:nian];
}

@end
